package com.ftg.learn.chapter09.pattern.factory;

/**
 * 课程工厂类
 * 为你生产课程的一个类
 *
 * @author kangjx
 * @date 2023/1/17
 */
public class CourseFactory<T extends AbstractCourse> {

    /**
     * 面向父类编程...
     * 多态 :
     *
     * @param name
     * @return
     */
    public static AbstractCourse getCourse(String name) {
        switch (name) {
            case "语言":
                Chinese pSchool = new Chinese();
                pSchool.setCourseName("语文");
                pSchool.setTeacherName("张三");
                pSchool.setDesc("这是小学语文");
                return pSchool;
            case "数学":
                Math m = new Math();
                m.setCourseName("数学");
                m.setTeacherName("李四");
                m.setDesc("这是小学数学");
                return m;
            case "英语":
                English me = new English();
                me.setCourseName("英语");
                me.setTeacherName("王五");
                me.setDesc("这是小学英语");
                return me;
            default:
                return null;

        }
    }

    /**
     * 面向接口编程
     * AbstractCourse  接口
     * jdk1.8 抽象方法
     * default 就是让别人继承
     *
     * @param name
     * @return
     */
    public static AbstractCourse getCourse(CourseTypeEnum name) {
        if (name == CourseTypeEnum.chinese) {
            Chinese pSchool = new Chinese();
            pSchool.setCourseName("语文");
            pSchool.setTeacherName("张三");
            pSchool.setDesc("这是小学语文");
            return pSchool;
        } else if (name == CourseTypeEnum.math) {
            Math m = new Math();
            m.setCourseName("数学");
            m.setTeacherName("李四");
            m.setDesc("这是小学数学");
            return m;
        } else if (name == CourseTypeEnum.english) {
            English me = new English();
            me.setCourseName("英语");
            me.setTeacherName("王五");
            me.setDesc("这是小学英语");
            return me;
        }
        return null;
    }

    /**
     * 反射方式，
     *
     * @param c           必须为AbstractCourse子类，报错ClassCastException类型转换异常
     * @param courseName
     * @param teacherName
     * @param desc
     * @return
     */
    public static AbstractCourse getCourse(Class c, String courseName, String teacherName, String desc) {
        try {
            AbstractCourse course = (AbstractCourse) c.newInstance();
            course.setCourseName(courseName);
            course.setTeacherName(teacherName);
            course.setDesc(desc);
            return course;
        } catch (InstantiationException | IllegalAccessException e) {
            e.printStackTrace();
        }
        return null;
    }


}
